import 'package:bpb/db/db_helper.dart';
import 'package:bpb/framework/base/base_controller.dart';
import 'package:bpb/framework/utils/date_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class AllController extends BaseController {

  int totalCount = 0;
  int pageIndex = 1;
  int pageSize = 10;
  var data = [].obs;

  var dateRangeTitle="".obs;

  @override
  void onInit() {
    super.onInit();
    DBHelper.getBpRecordCount().then((count) {
      totalCount = count;
      debugPrint("总数据条数:${totalCount}");
      loadData(true);
    });
  }

  loadData(bool isRefresh) async {
    if (isRefresh) {
      pageIndex = 1;
    } else {
      pageIndex++;
    }

    List<Map<String, Object?>> records = await DBHelper.queryByPage(pageSize, (pageIndex - 1) * pageSize);
    debugPrint("数据条数：${records.length}");

    if(isRefresh){
      data.value.clear();
      data.value.addAll(records);
    }else{
      data.value.addAll(records);
    }
    data.value=List.of(data.value);


    var dateStart=DateUtil.DF_YYYY_MM_DD.parse(data.value[data.value.length-1]["date"]);
    var dateEnd=DateUtil.DF_YYYY_MM_DD.parse(data.value[0]["date"]);

    dateRangeTitle.value="${DateUtil.DD_MM_YYYY.format(dateStart)} - ${DateUtil.DD_MM_YYYY.format(dateEnd)}";


    return records.length;

  }
}
